package com.baymax.study.base.utils;

import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.Predicate;
import com.querydsl.core.types.dsl.BooleanExpression;
import org.apache.commons.lang3.ObjectUtils;

import java.util.function.Function;

public class QueryDSLBuilder {
    private BooleanBuilder builder;

    private QueryDSLBuilder() {
    }

    public static QueryDSLBuilder builder() {
        QueryDSLBuilder dslBuilder = new QueryDSLBuilder();
        dslBuilder.builder = new BooleanBuilder();
        return dslBuilder;
    }

    public <T> QueryDSLBuilder notEmptyAnd(Function<T, BooleanExpression> expression, T value) {
        if (ObjectUtils.isNotEmpty(value)) {
            builder.and(expression.apply(value));
        }
        return this;
    }

    public Predicate build() {
        return builder.getValue();
    }

}
